<template>
  <el-dialog
    :title="'修改'+ titleText"
    :visible.sync="editParamDialog"
    width="50%"
    @close="editParamClose"
  >
    <el-form :model="paraminfo" :rules="editParamRules" ref="editParamRef" label-width="100px">
      <el-form-item :label="titleText" prop="attr_name">
        <el-input v-model="paraminfo.attr_name"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="editParamDialog = false">取 消</el-button>
      <el-button type="primary" @click="editParams">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: {
    visible: {
      type: Boolean
    },
    cateId: {
      type: Number
    },
    paraminfo: {
      type: Object
    },
    activeName: {
      type: String
    }
  },
  data() {
    return {
      editParamDialog: false,
      editParamRules: {
        attr_name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    editParamClose() {
      this.$refs.editParamRef.resetFields()
    },
    editParams() {
      this.$refs.editParamRef.validate(async valid => {
        if (!valid) return false
        const { data: res } = await this.$http.put(
          `categories/${this.cateId}/attributes/${this.paraminfo.attr_id}`,
          {
            attr_name: this.paraminfo.attr_name,
            attr_sel: this.activeName
          }
        )
        if (res.meta.status !== 200) {
          return this.$message.error('修改' + this.titleText + '失败')
        }
        this.$emit('editParamSuccess')
        this.editParamDialog = false
      })
    }
  },
  computed: {
    titleText: function() {
      if (this.activeName === 'many') {
        return '动态参数'
      }
      return '静态属性'
    }
  },
  watch: {
    visible: function(val) {
      this.editParamDialog = val
    },
    editParamDialog: function(val) {
      this.$emit('update:visible', val)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
